//
//  SecurityCenterView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import SnapKit
class SecurityCenterView: BaseView, UITableViewDataSource, UITableViewDelegate {
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
    lazy var nameArr:NSArray = [[kText("登录密码"),kText("手机绑定"),kText("支付密码"),kText("谷歌认证"),kText("邮箱绑定"),kText("解屏密码"),kText("身份认证")]]
    var viewModel:SecurityCenterViewModel?

    
    override func setUpUI() -> Void {
        viewModel = dataSource as? SecurityCenterViewModel
        self.themeBackground = ThemeManager.ThemeColorKeys.background
        
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.themeBackground = ThemeManager.ThemeColorKeys.background
        tableView.delegate = self
        tableView.dataSource = self
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
        
    }
    
    func setUpModel(model:SecurityModel?) {
        viewModel?.model = model!
        
        tableView.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let arr:NSArray = nameArr.object(at: section) as! NSArray
        
        return arr.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SecurityCenterCell = BaseCellFactory .createTableViewCellWithModel(model: BaseModel(), tableView: tableView, style: .SecurityStyleCell) as!SecurityCenterCell
        cell.setArrowImage()
        let arr:NSArray = nameArr.object(at: indexPath.section) as! NSArray
        cell.nameLabel.text = arr.object(at: indexPath.row) as? String
        cell.statesLabel.text = viewModel?.archiveStates(row:indexPath.row)
        return cell
    }
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 0 : 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 10))
        if ThemeManager.default.currentThemeMode() == ThemeMode.night {
            view.backgroundColor = UIColor(hexStr:"#262c37")
        }else {
            
            view.backgroundColor = UIColor(hexStr:"#f8f8f8")
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.row == 0 {
            let controller = SetPassWordViewController()
            controller.viewModel?.model = self.viewModel?.model
            controller.viewModel?.isLoadBool = true
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
            
        }else if indexPath.row == 1 {
            let controller = BindTelphoneViewController()
            controller.viewModel?.bindGoogleBool = viewModel?.model.level == bindLevel.googleLevel ? true : false
            controller.viewModel?.model = self.viewModel?.model
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
        }else if indexPath.row == 2 {
            if !(viewModel?.model.isGoogleBind)! {
                if !(viewModel?.model.isMobileBind)! {
                    CYMBShow.showMessage(kText("设置支付密码需要先开启谷歌认证或绑定手机"))
                    return
                }
            }
            let controller = SetPassWordViewController()
            controller.viewModel?.model = self.viewModel?.model
            controller.viewModel?.isLoadBool = false
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
        }else if indexPath.row == 3 {
            if !(self.viewModel?.model.isGoogleBind)! {
                let controller = GoogleViewController()
                controller.viewModel?.model = viewModel?.model
                viewModel?.controller?.navigationController?.pushViewController(controller, animated: true)
                return
            }
            
            let controller = GoogleOpenViewController()
            controller.viewModel?.model = self.viewModel?.model
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
        }else if indexPath.row == 4 {
            if (viewModel?.model.isEmailBind)! {
                CYMBShow.showMessage(kText("已绑定邮箱，请勿重复绑定"))
                return
            }
            let controller = BindEmailViewController()
            controller.viewModel?.model = self.viewModel?.model
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
        }else if indexPath.row == 5 {
            let controller = OpenLockViewController()
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
            
        }else if indexPath.row == 6{
            let controller = CertificationStatesViewController()
            self.dataSource.controller?.navigationController?.pushViewController(controller, animated: true)
        }
//        var arr:Array<UIViewController> = [SetPassWordViewController(),BindTelphoneViewController(), CertificationStatesViewController(),GoogleOpenViewController()]
//        let controller:UIViewController = arr[indexPath.row]
        
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

